/**
 * 2016，thor.com，all rights reserved。
 * 
 * Project：		palmmall-core
 * Filename：	ARepairServiceImpl.java
 * Created：		2016年9月14日
 * Creator：		wolv
 */
package com.thor.palmmall.rest.workorder;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;

import com.thor.commons.entity.BeanOperateInfo;
import com.thor.commons.exception.ThorServiceException;
import com.thor.commons.query.QueryResult;
import com.thor.commons.util.Assert;
import com.thor.palmmall.rest.common.AUserGroupLoadParam;
import com.thor.palmmall.rest.common.SUserGroupLoadParamConverter;
import com.thor.palmmall.rest.workorder.converter.AWorkOrderBillConverter;
import com.thor.palmmall.rest.workorder.converter.SWorkOrderBillConverter;
import com.thor.palmmall.rest.workorder.converter.SWorkOrderBillQueryParamConverter;
import com.thor.palmmall.spi.common.SOperateInfoConverter;
import com.thor.palmmall.spi.common.SQueryResult;
import com.thor.palmmall.spi.common.SQueryResultConverter;
import com.thor.palmmall.spi.common.SResponse;
import com.thor.palmmall.spi.workorder.SWorkOrderBill;
import com.thor.palmmall.spi.workorder.SWorkOrderService;

/**
 * @author kennen
 *
 */
public class AWorkOrderServiceImpl implements AWorkOrderService {

  @Override
  public String[] queryOutgoingState() throws ThorServiceException {
    SResponse<String[]> resp = sWorkOrderService.queryOutgoingState();
    if (resp.isSuccess()) {
      return resp.getBody();
    } else {
      throw new ThorServiceException("查询工单流程状态失败：" + resp.getMessage());
    }
  }

  @Override
  public String create(AWorkOrderBill bill, BeanOperateInfo operateInfo)
      throws ThorServiceException {

    Assert.assertArgumentNotNull(bill, "bill");
    Assert.assertArgumentNotNull(operateInfo, "operateInfo");
    Assert.assertArgumentNotNull(operateInfo.getOperator(), "operateInfo.getOperator()");
    Assert.assertArgumentNotNull(operateInfo.getTime(), "operateInfo.getTime()");

    SResponse<String> resp = sWorkOrderService.create(
        SWorkOrderBillConverter.getInstance().convert(bill),
        SOperateInfoConverter.getInstance().convert(operateInfo));
    if (resp.isSuccess())
      return resp.getBody();
    else
      throw new ThorServiceException("无法创建工单：" + resp.getMessage());
  }

  @Autowired
  private SWorkOrderService sWorkOrderService;

  @Override
  public String save(AWorkOrderBill bill, BeanOperateInfo operateInfo) throws ThorServiceException {

    Assert.assertArgumentNotNull(bill, "bill");
    Assert.assertArgumentNotNull(operateInfo, "operateInfo");
    Assert.assertArgumentNotNull(operateInfo.getOperator(), "operateInfo.getOperator()");
    Assert.assertArgumentNotNull(operateInfo.getTime(), "operateInfo.getTime()");

    SResponse<String> resp = sWorkOrderService.save(
        SWorkOrderBillConverter.getInstance().convert(bill),
        SOperateInfoConverter.getInstance().convert(operateInfo));
    if (resp.isSuccess())
      return resp.getBody();
    else
      throw new ThorServiceException("无法保存工单：" + resp.getMessage());
  }

  @Override
  public AWorkOrderBill get(String id, AUserGroupLoadParam param) throws ThorServiceException {
    SResponse<SWorkOrderBill> resp = sWorkOrderService.get(id,
        SUserGroupLoadParamConverter.getInstance().convert(param));
    if (resp.isSuccess())
      return AWorkOrderBillConverter.getInstance().convert(resp.getBody());
    else
      throw new ThorServiceException("无法找到指定ID的工单：" + resp.getMessage());
  }

  @Override
  public QueryResult<AWorkOrderBill> query(AWorkOrderBillQueryParam param)
      throws ThorServiceException {
    SResponse<SQueryResult<SWorkOrderBill>> resp = sWorkOrderService
        .query(SWorkOrderBillQueryParamConverter.getInstance().convert(param));
    if (resp.isSuccess())
      return SQueryResultConverter.convert(resp.getBody(), AWorkOrderBillConverter.getInstance());
    else
      throw new ThorServiceException("无法查询到工单：" + resp.getMessage());
  }

  @Override
  public Map<String, String> getAllTypes() throws ThorServiceException {
    SResponse<Map<String, String>> resp = sWorkOrderService.getAllTypes();
    if (resp.isSuccess()) {
      return resp.getBody();
    } else {
      throw new ThorServiceException("查询工单支持的业务单类型失败：" + resp.getMessage());
    }
  }
}
